﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using DXApplicationTangche.UC.货号管理.service;
using DevExpress.XtraGrid.Demos.util;

namespace DXApplicationTangche.UC.货号管理
{
    public partial class Form款式添加工艺 : DevExpress.XtraEditors.XtraForm
    {
        private String styleSonCode;
        private String partId;//位置Id
        private String partDesc;//位置描述
        private String craftDesignId;//工艺设计点id
        private List<String> craftIds = new List<string>();//款式工艺设计id
        private List<CraftDesignStyleRelationDTO> craftDesignStyleRelationDTOs工艺 = new List<CraftDesignStyleRelationDTO>();
        public Form款式添加工艺(String styleSonCode,String craftDesignId,String craftDesignDesc)
        {
            InitializeComponent();
            this.styleSonCode = styleSonCode;
            this.craftDesignId = craftDesignId;
            this.searchLookUpEdit选择位置.Properties.DataSource = 款式迭代Service.getAllGarmentPartDaos();
        }
        public Form款式添加工艺(List<CraftDesignStyleRelationDTO> craftDesignStyleRelationDTOs工艺, List<String> craftDesignIds)
        {
            InitializeComponent();
            this.craftDesignStyleRelationDTOs工艺 = craftDesignStyleRelationDTOs工艺;
            this.craftIds = craftDesignIds;
            this.searchLookUpEdit选择位置.Properties.DataSource = 款式迭代Service.getAllGarmentPartDaos();
        }

        #region 选择位置
        private void searchLookUpEdit选择位置_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn位置_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit选择位置View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit选择位置.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.partId = this.searchLookUpEdit选择位置.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.partDesc = this.searchLookUpEdit选择位置.Properties.View.GetRowCellValue(rowHandle, "partDesc").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn位置_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择位置.ToolTip = null;
            searchLookUpEdit选择位置.EditValue = null;
        }
        private void searchLookUpEdit选择位置_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.partDesc;
        }
        #endregion

        private void simpleButton添加工艺到款式_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.partId))
            {
                MessageBox.Show("选择位置");
                return;
            }
            try
            {
                foreach (CraftDesignStyleRelationDTO item in this.craftDesignStyleRelationDTOs工艺)
                {
                    for (int i = 0; i < this.craftIds.Count; i++)
                    {
                        if (item.id.Equals(this.craftIds[i]))
                        {
                            item.partId = this.partId;
                            item.partDesc = this.partDesc;
                            break;
                        }
                    }
                }
                //String re =
                //    款式迭代Service.saveCraftDesignStyleRelation(
                //        new FunctionHelper().Uuid,
                //        this.styleSonCode,
                //        this.partId,
                //        this.craftDesignId
                //    );
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.Close();
        }
    }
}